package com.fh.db.oracle;

import com.fh.db.*;
import com.fh.db.base.BaseDbTable;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author qiujunda
 * @Date 20/07/2020
 * @Description
 */
public class OracleDbTable extends BaseDbTable {

    @Override
    public DbSource getDbSource() {
        return dbSource;
    }

    public void setDbSource(DbSource dbSource) {
        this.dbSource = dbSource;
    }

    private DbSource dbSource;

    private String tablespaceName;

    public String getTablespaceName() {
        return tablespaceName;
    }

    public void setTablespaceName(String tablespaceName) {
        this.tablespaceName = tablespaceName;
    }

    @Override
    public List<DbColumn> getColumns() {
        return (List<DbColumn>) SqlRunner.preparedSqlResult(dbSource,dbSource.getSqlSet().columnPreSql(),new String[]{this.getTableName(),this.getOwner()},
                resultSet -> {
                    List<DbColumn> result=new ArrayList<>();
                    try{
                        while(resultSet.next()){
                            OracleDbColumn oracleDbColumn=new OracleDbColumn();
                            oracleDbColumn.setColumnId(resultSet.getBigDecimal("COLUMN_ID").intValue());
                            oracleDbColumn.setColumnName(resultSet.getString("COLUMN_NAME"));
                            oracleDbColumn.setColumnComments(resultSet.getString("COMMENTS"));
                            oracleDbColumn.setNullable(resultSet.getString("NULLABLE"));
                            oracleDbColumn.setDataType(resultSet.getString("DATA_TYPE"));
                            oracleDbColumn.setDataLength(resultSet.getBigDecimal("DATA_LENGTH").intValue());
                            BigDecimal t=resultSet.getBigDecimal("DATA_PRECISION");
                            oracleDbColumn.setDataPrecision(t==null?null:t.intValue());
                            result.add(oracleDbColumn);
                        }
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                    return result;
                });
    }

    @Override
    public List<DbTrigger> getTriggers() {
        return null;
    }

    @Override
    public List<DbIndex> getIndexs() {
        return (List<DbIndex>) SqlRunner.preparedSqlResult(dbSource,dbSource.getSqlSet().indexPreSql(),new String[]{this.getTableName(),this.getOwner()},
                resultSet -> {
                    List<DbIndex> result=new ArrayList<>();
                    try{
                        while(resultSet.next()){
                            OracleDbIndex oracleDbIndex=new OracleDbIndex();
                            oracleDbIndex.setDbSource(dbSource);
                            oracleDbIndex.setIndexName(resultSet.getString("INDEX_NAME"));
                            oracleDbIndex.setIndexType(resultSet.getString("INDEX_TYPE"));
                            oracleDbIndex.setOwner(resultSet.getString("OWNER"));
                            oracleDbIndex.setUniqueness(resultSet.getString("UNIQUENESS"));
                            result.add(oracleDbIndex);
                        }
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                    return result;
                });
    }
}
